﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net;

public partial class 代码段仓库_下载图片到服务器端_DownLoadPictures : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //

        string r=SaveAsWebImg("http://www.furongedu.com/r/cms/www/tpl1/images/logo.jpg");
        if (!string.IsNullOrEmpty(r))
        {
            Image1.ImageUrl = "~/upload/pic/" + r;
            Image1.DataBind();
        }
            
    }

    /// <summary>  
    /// 下载网站图片,如果图片不存在，则返回空值，返回空值，就表示图片不存在。  
    /// </summary>  
    /// <param name="picUrl"></param>  
    /// <returns></returns>  
    public string SaveAsWebImg(string picUrl)
    {
        string result = "";

        string imgtype = System.IO.Path.GetExtension(picUrl).ToLower();

        if (String.IsNullOrEmpty(picUrl) || String.IsNullOrEmpty(imgtype))
        {
            return result;
        }

        string sfilename = DateTime.Now.Ticks.ToString() + imgtype;
        string sfile = HttpContext.Current.Server.MapPath("~/upload/pic/" + sfilename);

        try
        {
            WebClient webClient = new WebClient();   //命名空间为System.Net
            webClient.DownloadFile(picUrl,sfile);
            result = sfilename;
        }
        catch { }
        return result;
    }  
}